using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using Oarw.General;

namespace ScreenMonkey.Plugin.Clips.Song
{
    public partial class SongEdittor : UserControl, ICopyAndPaste, IEdit
    {
        private bool loading = false;
        private Dictionary<string, ToolStripMenuItem> addLabelItems = new Dictionary<string, ToolStripMenuItem>();
        private List<ToolStripButton> labelButtons = new List<ToolStripButton>();

        public event EventHandler TitleChanged;

        public SongEdittor()
        {
            InitializeComponent();

            labelStrip.Height = 18;

            SongLabels.LabelAdded += new EventHandler(SongLabels_LabelAdded);
        }

        void SongLabels_LabelAdded(object sender, EventArgs e)
        {
            if(IsHandleCreated) BeginInvoke(new VoidHandler(UpdateLabels));
        }

        public void LoadLyrics()
        {
            if (songWords.Lyrics.Title != "")
                this.Text = songWords.Lyrics.Title + " - Song Edit";
            else
                this.Text = "Song Edit";

            titleText.Text = songWords.Lyrics.Title;
            authorTxt.Text = songWords.Lyrics.Author;
            ownerTxt.Text = songWords.Lyrics.Owner;
            yearTxt.Text = songWords.Lyrics.Year;
            referenceTxt.Text = songWords.Lyrics.Reference;

            Lyrics.LabelAdded += Lyrics_LabelChanged;
            Lyrics.LabelRemoved += Lyrics_LabelChanged;
            Lyrics.SequenceChanged += Lyrics_SequenceChanged;

            UpdateVerseOrder();
            UpdateLabels();
        }

        void Lyrics_SequenceChanged(object sender, EventArgs e)
        {
            UpdateVerseOrder();
        }

        public void UpdateVerseOrder()
        {
            if (InvokeRequired)
            {
                BeginInvoke(new VoidHandler(UpdateVerseOrder));
                return;
            }

            //Update the sequence text and maintain cursor position.
            int cursorPosition = sequenceTxt.SelectionStart;
            int length = sequenceTxt.SelectionLength;
            sequenceTxt.Text = songWords.Lyrics.VerseOrder();
            sequenceTxt.Select(cursorPosition, length);
        }

        void Lyrics_LabelChanged(object sender, EventArgs e)
        {
            UpdateLabels();
            Modified = true;
        }

        protected void UpdateLyrics()
        {
            if (songWords.Lyrics != null)
            {
                songWords.Lyrics.Title = titleText.Text;
                songWords.Lyrics.Author = authorTxt.Text;
                songWords.Lyrics.Owner = ownerTxt.Text;
                songWords.Lyrics.Year = yearTxt.Text;
                songWords.Lyrics.Reference = referenceTxt.Text;
            }
        }

        protected void UpdateLabels()
        {
            //Invoke onto native thread if required.
            if (InvokeRequired)
            {
                BeginInvoke(new VoidHandler(UpdateLabels));
                return;
            }

            //Add all label catagories to the add label list.
            foreach(string label in SongLabels.Items)
            {
                if (!addLabelItems.ContainsKey(label))
                {
                    ToolStripMenuItem selectItem = new ToolStripMenuItem(label);
                    selectItem.Click += new EventHandler(selectItem_Click);
                    selectItem.Tag = label;

                    addLabel.DropDownItems.Insert(0, selectItem);
                    addLabelItems.Add(label, selectItem);
                }
            }    

            //Clear all the old label buttons.
            foreach (ToolStripButton button in labelButtons)
            {
                labelStrip.Items.Remove(button);
            }
            labelButtons.Clear();

            if (Lyrics != null)
            {
                //Add labels relatting to this song.
                foreach (string label in Lyrics.Labels)
                {
                    ToolStripButton newButton = new ToolStripButton(label, Properties.Resources.CrossSmall);

                    newButton.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
                    newButton.TextImageRelation = TextImageRelation.TextBeforeImage;
                    newButton.BackColor = Color.DarkOrange;
                    newButton.ForeColor = Color.White;
                    newButton.Margin = new Padding(0, 3, 0, 3);
                    newButton.Font = new Font(newButton.Font, FontStyle.Bold);

                    newButton.Tag = label;

                    newButton.Click += new EventHandler(newButton_Click);

                    labelStrip.Items.Insert(0, newButton);
                    labelButtons.Add(newButton);
                }
            }
        }

        void newButton_Click(object sender, EventArgs e)
        {
            ToolStripButton selectedItem = sender as ToolStripButton;
            if (selectedItem != null)
            {
                string label = selectedItem.Tag as string;
                Lyrics.RemoveLabel(label);
            }
        }

        void selectItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem selectedItem = sender as ToolStripMenuItem;
            if (selectedItem != null)
            {
                string label = selectedItem.Tag as string;
                Lyrics.AddLabel(label);
            }
        }

        public SongLyrics Lyrics
        {
            get
            {
                UpdateLyrics();

                return songWords.Lyrics;
            }
            set
            {
                loading = true;
                songWords.Lyrics = new SongLyrics(value);
                LoadLyrics();
                loading = false;

                Modified = false;
            }
        }


        #region ICopyAndPaste Members

        public void Cut()
        {
            if (this.ActiveControl is ICopyAndPaste) ((ICopyAndPaste)this.ActiveControl).Cut();
            else if(this.ActiveControl is TextBoxBase) ((TextBoxBase) this.ActiveControl).Cut();
        }

        public void Copy()
        {
            if (this.ActiveControl is ICopyAndPaste) ((ICopyAndPaste)this.ActiveControl).Copy();
            else if (this.ActiveControl is TextBoxBase) ((TextBoxBase)this.ActiveControl).Copy();
        }

        public void Paste()
        {
            if (this.ActiveControl is ICopyAndPaste) ((ICopyAndPaste)this.ActiveControl).Paste();
            else if (this.ActiveControl is TextBoxBase) ((TextBoxBase)this.ActiveControl).Paste();
        }
        
        public void SelectAll()
        {
            if (this.ActiveControl is ICopyAndPaste) ((ICopyAndPaste)this.ActiveControl).SelectAll();
            else if (this.ActiveControl is TextBox) ((TextBox)this.ActiveControl).SelectAll();
        }

        #endregion

        #region IEdit Members

        public event EventHandler ModifiedChanged;

        private bool modified = false;

        public bool Modified
        {
            get { return modified | songWords.Modified; }
            set
            {
                if ((modified != value || songWords.Modified != value)&& !loading)
                {
                    modified = value;
                    if (value == false) songWords.Modified = false;
                    OnModifiedChanged();
                }
                
            }
        }

        #endregion

        private void titleText_TextChanged(object sender, EventArgs e)
        {
            Modified = true;
            if (TitleChanged != null) TitleChanged(this, EventArgs.Empty);
        }

        private void authorTxt_TextChanged(object sender, EventArgs e)
        {
            Modified = true;
        }

        private void ownerTxt_TextChanged(object sender, EventArgs e)
        {
            Modified = true;
        }

        private void referenceTxt_TextChanged(object sender, EventArgs e)
        {
            Modified = true;
        }

        private void yearTxt_TextChanged(object sender, EventArgs e)
        {
            Modified = true;
        }

        private void sequenceTxt_TextChanged(object sender, EventArgs e)
        {
            if (!loading && songWords.Lyrics.VerseOrder() != sequenceTxt.Text)
            {
                songWords.Lyrics.UpdateVerseOrder(sequenceTxt.Text);
                Modified = true;
            }            
        }

        private void songWords_ModifiedChanged(object sender, EventArgs e)
        {
            OnModifiedChanged();
        }

        protected void OnModifiedChanged()
        {
            if (ModifiedChanged != null) ModifiedChanged(this, EventArgs.Empty);
        }

        private void addLabel_DropDownClosed(object sender, EventArgs e)
        {
            if (newLabel.Text != string.Empty)
            {
                Lyrics.AddLabel(newLabel.Text);

            }

            newLabel.Text = string.Empty;
        }

        private void newLabel_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                addLabel.DropDown.Close();
            }
        }

    }
}
